package com.Txunda.fxw.ui.activity.other;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.config.Settings;
import com.Txunda.fxw.ui.adapter.MyRecyclerAdapter;
import com.Txunda.fxw.ui.adapter.model.TeaHouse;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.http.AboutUs;
import com.Txunda.fxw.ui.http.Index;
import com.Txunda.fxw.ui.toolkit.util.DensityUtil;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.ui.widget.XCSlideView;
import com.Txunda.fxw.view.RoundedImageView;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 添加侧滑功能
 *
 * @author Administrator
 */
public class MessageInfoDetailAty extends BaseAty {

    private Index index;

    @ViewInject(R.id.aty_title3_tv_center)
    private TextView aty_title3_tv_center;
    @ViewInject(R.id.aty_title3_tv_right2)
    private TextView aty_title3_tv_right2;
    @ViewInject(R.id.icon_img)
    private RoundedImageView icon_img;
    private List<Map<String, String>> list_product = new ArrayList<>();
    private Bundle bundle;
    private XCSlideView mSlideViewRight;

    private RecyclerView main_recycler;
    private MyRecyclerAdapter adapter;

    private TextView tv_weibo;

    private TextView tv_link;

    @ViewInject(R.id.tea_list_lv)
    private ListView tea_list_lv;
    private MyAdapter teaAdapter;
    private AboutUs aboutUs;

    private ArrayList<Map<String, String>> list_data;
    private List<TeaHouse> list_model;
    private ArrayList<Map<String, String>> recycler_list_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        showProgressDialog();

        index.teaHouse(this);

        tea_list_lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                bundle = new Bundle();
                // bundle.putString("from", "1");
                bundle.putString("id", list_data.get(position).get("id"));
                ArrayList<Map<String, String>> map_list = JSONUtils.parseKeyAndValueToMapList(list_data.get(position).get("name"));
                if (ListUtils.isEmpty(map_list))
                    return;
                for (int i = 0; i < map_list.size(); i++) {
                    bundle.putString("title", map_list.get(i).get("list_name"));
                }

                startActivity(SingleDetailAty.class, bundle);
            }
        });

    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map))
            return;
        if (requestUrl.contains("introduce")) { // 介绍信息
            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data))
                return;
            String microblog = data.get("microblog"); // 微博
            final String url = data.get("url");
            recycler_list_data = JSONUtils.parseKeyAndValueToMapList(data.get("list"));
            tv_weibo.setText(microblog);

            tv_link.setText(url);
            adapter.setList(recycler_list_data);
            main_recycler.setAdapter(adapter);

            tv_link.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);

                }
            });
        }
        if (requestUrl.contains("teaHouse")) {
            list_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
            if (ListUtils.isEmpty(list_data))
                return;
            for (Map<String, String> temp : list_data) {
                list_model.add(new TeaHouse(temp.get("id"), temp.get("ad_pic"),
                        JSONUtils.parseKeyAndValueToMapList(temp.get("name"))));
            }
            tea_list_lv.setAdapter(teaAdapter);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_message_info_detail_aty;
    }

    @Override
    @OnClick({R.id.icon_img})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

            case R.id.icon_img: // logo
                if (!mSlideViewRight.isShow())
                    mSlideViewRight.show();
                aboutUs.introduce(this);
                break;

            default:
                break;
        }
    }

    @Override
    protected void initialized() {
        recycler_list_data = new ArrayList<>();
        aboutUs = new AboutUs();
        list_product = new ArrayList<>();
        teaAdapter = new MyAdapter();
        list_model = new ArrayList<>();
        String logo = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_logo);
        Glide.with(this).load(logo).into(icon_img);
        list_data = new ArrayList<>();
        index = new Index();

        aty_title3_tv_right2.setVisibility(View.VISIBLE);
        Context mContext = this;
        // 屏幕宽度
        int mScreenWidth = DensityUtil.getScreenWidthAndHeight(mContext)[0];
        mSlideViewRight = XCSlideView.create(this, XCSlideView.Positon.RIGHT);
        View menuViewRight = LayoutInflater.from(mContext).inflate(R.layout.layout_slideview, null);
        TextView tv_notice = menuViewRight.findViewById(R.id.tv_notice);
        TextView tv_comment = menuViewRight.findViewById(R.id.tv_comment);
        mSlideViewRight.setMenuView(MessageInfoDetailAty.this, menuViewRight);
        main_recycler = menuViewRight.findViewById(R.id.main_recycler);
        tv_link = menuViewRight.findViewById(R.id.tv_link);
        mSlideViewRight.setMenuWidth(mScreenWidth * 6 / 9);
        tv_weibo = menuViewRight.findViewById(R.id.tv_weibo);
        // 公告
        tv_notice.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(NoticeAty.class, null);
            }
        });

        tv_weibo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showTips(R.drawable.error, "敬请期待");
            }
        });
        tv_comment.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(LeaveMessageAty.class, null);
            }
        });
        aty_title3_tv_center.setText("一元茶座");
        String area = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_mode);
        aty_title3_tv_right2.setText(area);

        // 设置LinearLayoutManager
        main_recycler.setLayoutManager(
                new LinearLayoutManager(MessageInfoDetailAty.this, LinearLayoutManager.HORIZONTAL, false));
        main_recycler.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        main_recycler.setHasFixedSize(true);
        adapter = new MyRecyclerAdapter(MessageInfoDetailAty.this);
    }

    @Override
    protected void requestData() {

    }

    /**
     * 自定义adapter
     *
     * @author Administrator
     */
    public class MyAdapter extends BaseAdapter {

        private MyViewHolder vh;

        @Override
        public int getCount() {
            int ret = 0;
            if (list_model != null) {
                ret = list_model.size();
            }
            return ret;
        }

        @Override
        public TeaHouse getItem(int position) {
            return list_model.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(MessageInfoDetailAty.this).inflate(R.layout.item_index, parent, false);
                vh = new MyViewHolder();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (MyViewHolder) convertView.getTag();
            }
            LayoutParams params = new LayoutParams(Settings.displayWidth, Settings.displayHeight / 3);
            vh.item_img1.setLayoutParams(params);
            TeaHouse teaHouse = getItem(position);

            Glide.with(MessageInfoDetailAty.this).load(teaHouse.getAd_pic()).into(vh.item_img1);
            list_product = teaHouse.getList_data();
            // 移除布局
            if (vh.item_layout != null) {
                vh.item_layout.removeAllViews();
            }
            for (int i = 0; i < list_product.size(); i++) {
                LinearLayout layout = (LinearLayout) LayoutInflater.from(MessageInfoDetailAty.this).inflate(R.layout.item_tv, null);
                TextView item_tv = layout.findViewById(R.id.item_tv);
                item_tv.setText("「 " + list_product.get(i).get("list_name") + "」");
                vh.item_layout.addView(layout);
            }
            return convertView;
        }

        class MyViewHolder {
            @ViewInject(R.id.item_img1)
            private ImageView item_img1;
            @ViewInject(R.id.linear_list)
            private LinearLayout item_layout;
            @ViewInject(R.id.item_frame)
            private FrameLayout item_frame;
        }

    }

}
